import json
import requests

from comm.dbConnect import dbJonOne
from setting import openDbBase

class apiList:
    dbproconfig = openDbBase()
    # compno = dbproconfig['COMPNO']
    # compno = "NYJS"
    # 广播标签名称
    # broadcastName = "data"
    # broadcastName = dbproconfig['BROADCASTNAME']
    # apiAdd = dbproconfig['APIADD']
    # 接口授权秘钥
    # clientsecret = "5885fc22f3764bfc85c18cdc36ac30a7"
    clientsecret = dbproconfig['CLIENTSECRET']
    # 接口授权Id
    # clientid = "001"
    clientid = dbproconfig['CLIENTID']
    # 企业编码
    # entCode = "012"
    entCode = dbproconfig['ENTCODE']
    # 组织编码(接口授权Id)
    # orgCode = "001"
    orgCode = dbproconfig['ORGCODE']
    # apiAdd = "http://u9erp.csboda.com.cn:8903/U9C/webapi/"
    apiAdd = dbproconfig['APIADD']

    # "d5e2a970-1e41-42ca-a639-118931f14083"
    # 用户登录pyinstaller -F serve.py
    @classmethod
    def login(cls,userCode):
        url = cls.apiAdd + "OAuth2/AuthLogin"
        # url = cls.apiAdd + "OAuth2/GetAuthorizeCode"
        header = {
            "Content-Type":"application/json"
        }
        params = {}
        params['clientsecret'] = cls.clientsecret
        params['clientid'] = cls.clientid
        params['entCode'] = cls.entCode
        params['orgCode'] = cls.orgCode
        params['userCode'] = userCode
        # print(url)
        r = requests.get(url,params=params,headers=header)
        if r.status_code != 200:
            # print(r.json())
            # print(r.status_code)
            return None, 'api接口登录请求失败'
        # print(r.status_code)
        resData = r.json()
        # print(resData)
        if resData['ResCode']!=0:
            return None,"账号不存在或密码错误 "+ str(resData['ResCode']) + resData['ResMsg']
        return resData['Data'],None

    def orgCodeConfig(cls,db):
        config_data = openDbBase()
        sql1 = "select id,code from Base_Organization where code='{0}'".format(cls.orgCode)
        data, err = dbJonOne(sql=sql1, engineName=None, parm={"code": cls.orgCode}, db_session=db)
        if data is None:
            return None,'未查询到组织编码'
        return data['id'], err
    # 新增工序转移单
    @classmethod
    def yaZhuDepAdd(cls,data,token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "OpTransferDoc/Create"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token":token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '新增接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '新增失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '新增失败' + resData['Data'][0]['m_errorMsg']
        # print(resData)
        data = {}
        data['id'] = resData['Data'][0]['m_iD']
        data['zy_no'] = resData['Data'][0]['m_code']
        return data, None

    # 审核工序转移单
    @classmethod
    def yaZhuDepSh(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "OpTransferDoc/Approve"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '审核接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '审核失败' + resData['Data'][0]['m_errorMsg']
        return None, None

    # 删除工序转移单
    @classmethod
    def yaZhuDepDelete(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "OpTransferDoc/Delete"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '删除接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '删除失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '删除失败' + resData['Data'][0]['m_errorMsg']
        return None, None

    # 生成生产订单
    @classmethod
    def yaZhuDepMoAdd(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MODoc/Create"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '新增生成订单接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '新增生成订单失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '新增生成订单失败' + resData['Data'][0]['m_errorMsg']
        # print(resData)
        # print(resData)
        data = {}
        data['id'] = resData['Data'][0]['m_iD']
        data['zy_no'] = resData['Data'][0]['m_code']
        return data, None

    # 审核生产订单
    @classmethod
    def yaZhuDepMoSh(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MODoc/ApproveMo"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '审核生成订单接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '审核生成订单失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '审核生成订单失败' + resData['Data'][0]['m_errorMsg']
        # print(resData)
        data = {}
        data['id'] = resData['Data'][0]['m_iD']
        data['zy_no'] = resData['Data'][0]['m_code']
        return data, None

    # 生产订单开工
    @classmethod
    def yaZhuDepMoStar(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MODoc/StartMo"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '生产订单开工接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '生产订单开工失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '生产订单开工失败' + resData['Data'][0]['m_errorMsg']
        return None, None

    # 生产完工报告单
    @classmethod
    def yaZhuDepMoEndr(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "CompleteDoc/Create"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '生成订单完工报告接口请求失败'
        resData = r.json()

        if resData['Success'] != True:
            return None, '生成订单完工报告失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '生成订单完工报告失败' + resData['Data'][0]['m_errorMsg']
        data = {}
        data['id'] = resData['Data'][0]['m_iD']
        data['no'] = resData['Data'][0]['m_code']
        # print(data,'生成完工报告单')
        return data, None

    # 修改完工报告单
    @classmethod
    def yaZhuDepMoEndrUpdate(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "CompleteDoc/Create"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '生成订单完工报告接口请求失败'
        resData = r.json()

        if resData['Success'] != True:
            return None, '生成订单完工报告失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '生成订单完工报告失败' + resData['Data'][0]['m_errorMsg']
        data = {}
        data['id'] = resData['Data'][0]['m_iD']
        data['no'] = resData['Data'][0]['m_code']
        # print(data, '生成完工报告单')
        return data, None

    # 完工报告单提交跟审核
    @classmethod
    def yaZhuDepMoEndrShenHe(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        # # 完工报告单提交
        # url = cls.apiAdd + "CompleteDoc/Approve"
        # jsonData = data
        # # pprint(jsonData)
        # # print(json.dumps(jsonData,ensure_ascii=False))
        # header = {
        #     "Content-Type": "application/json",
        #     "token": token
        # }
        # r = requests.post(url, json=jsonData, headers=header)
        # if r.status_code != 200:
        #     return None, '完工报告单提交接口请求失败'
        # resData = r.json()
        # print(resData)
        # if resData['Success'] != True:
        #     return None, '完工报告单提交失败' + resData['ResMsg']
        # if resData['Success'] == True:
        #     if resData['Data'][0]['m_isSucess'] == False:
        #         return None, '完工报告单提交失败' + resData['Data'][0]['m_errorMsg']

        # 完工报告单审核
        url = cls.apiAdd + "CompleteDoc/Approve"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '完工报告单审核接口请求失败'
        resData = r.json()
        print(resData)
        if resData['Success'] != True:
            return None, '完工报告单审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '完工报告单审核失败' + resData['Data'][0]['m_errorMsg']
        return data, None

    # 成品入库单修改
    def chengPingUpdate(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        # 成品入库单审核
        url = cls.apiAdd + "RcvRptDoc/Modify"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '成品入库单修改接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '成品入库单修改失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '成品入库单修改失败' + resData['Data'][0]['m_errorMsg']
        return data, None

    # 成品入库单审核
    def chengPingShenHe(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        # 成品入库单审核
        url = cls.apiAdd + "RcvRptDoc/Approve"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '成品入库单审核接口请求失败'
        resData = r.json()
        print(resData)
        if resData['Success'] != True:
            return None, '成品入库单审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['m_isSucess'] == False:
                return None, '成品入库单审核失败' + resData['Data'][0]['m_errorMsg']
        return data, None

    # 生成调入单
    @classmethod
    def diaoRuAdd(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "TransferIn/Create"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '新增生成调入单接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '新增生成调入单失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '新增生成调入单失败' + resData['Data'][0]['ErrorMsg']
        # print(resData)
        # print(resData)
        data = {}
        data['id'] = resData['Data'][0]['ID']
        data['no'] = resData['Data'][0]['Code']
        return data, None
    # 调入单删除
    @classmethod
    def diaoRuDelete(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "TransferIn/Delete"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '删除调入单接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '删除调入单失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '删除调入单失败' + resData['Data'][0]['ErrorMsg']
        # print(resData)
        # print(resData)
        data = {}
        data['id'] = resData['Data'][0]['ID']
        data['no'] = resData['Data'][0]['Code']
        return data, None

    # 调入单提交跟审核
    @classmethod
    def diaoRuTiJiaoShenHe(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        # 调入单提交
        url = cls.apiAdd + "TransferIn/Submit"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '调入单提交接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '调入单提交失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '调入单提交失败' + resData['Data'][0]['ErrorMsg']

        # 调入单审核
        url = cls.apiAdd + "TransferIn/Approve"
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "token": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '调入单审核接口请求失败'
        resData = r.json()
        # print(resData)
        if resData['Success'] != True:
            return None, '调入单审核失败' + resData['ResMsg']
        if resData['Success'] == True:
            if resData['Data'][0]['IsSucess'] == False:
                return None, '调入单审核失败' + resData['Data'][0]['ErrorMsg']
        return data, None

# apiList.login(2005140)
